<template>
	<view class="page">
		<flowbefore :beflows="beflows" :status="status" :isBtnClick="isBtnClicks" @save="save" @submit="submit" :statusType="statusType">
			<template>
				<view class="padding-bottom-100">
					<form class="flowform">
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>流程标题</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.flowTitle" name="flowTitle" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext ">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>单号编号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.billNo" name="billNo" disabled="true" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="flowUrgent">
							<view class="formgroup">
								<view class="labletitle"><label>紧急程度</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.flowUrgent" name="flowUrgent" placeholder="选择" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext margin-top-20">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>开票人</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.drawer" name="drawer" placeholder="输入开票人" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="billDate">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>开票日期</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.billDate" name="billDate" disabled="true" placeholder="选择" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>公司名称</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.companyName" name="companyName" placeholder="输入公司名称" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>关联名称</label></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.conName" name="conName" placeholder="输入关联名称" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>开户银行</label></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.bank" name="bank" placeholder="输入开户银行" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>开户账号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.amount" name="amount" placeholder="输入账号" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>开票金额</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="digit" v-model="conBill.billAmount" name="billAmount" placeholder="输入开票金额" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>税号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.taxId" name="taxId" placeholder="输入税号" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>发票号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.invoiceId" name="invoiceId" placeholder="输入发票号" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>付款金额</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="digit" v-model="conBill.payAmount" name="payAmount" placeholder="输入金额" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>发票地址</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="conBill.invoAddress" name="invoAddress" placeholder="输入发票地址" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist">
							<view class="formgroup  padding-17">
								<view class="labletitletar"><label>备注</label></view>
								<view class="controlvalue  uni-textarea">
									<textarea v-model="conBill.description" name="description" maxlength="25" :disabled="disabled" placeholder="输入备注" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext margin-top-20">
							<!-- <view class="formgroup">
								<view class="labletitle"><label>相关图片</label></view>
							</view> -->
						</view>
						<!-- <upload-deg :upImgConfig="upImgBasic" :fileList="fileList" @onUpImg="upBasicData" ref="uImage"></upload-deg> -->
						<JNPFUpload v-model="fileList" :fileSize='2' :limit='9'></JNPFUpload>
					</form>
					<view class="height-100"></view>
				</view>
			</template>
		</flowbefore>
		<w-picker :mode="mode" :pickerList="pickerList" ref="picker" :isSearchShow="isSearchShow"></w-picker>
	</view>
</template>

<script>
	import jnpf from '@/common/js/jnpf.js'
	import helper from '@/common/js/helper.js'
	import graceChecker from '@/common/js/graceChecker.js'
	import wPicker from '@/components/w-picker/w-picker.vue'
	import flowbefore from '@/components/work/flow-before.vue'
	
	import config from '@/common/js/config.js'
	let formRule=[
			{name:"flowTitle",checkType:"notnull",checkRule:"",errorMsg:"输入标题"},
			{name:"flowUrgent", checkType : "notnull", checkRule:"",  errorMsg:"选择紧急程度"},
			{name:"billDate", checkType : "notnull", checkRule:"",  errorMsg:"选择开票日期"},
			{name:"companyName", checkType : "notnull", checkRule:"",  errorMsg:"输入公司名称"},
			{name:"drawer", checkType : "notnull", checkRule:"",  errorMsg:"输入开票人"},
			{name:"billAmount", checkType : "money", checkRule:"",  errorMsg:"输入合格开票金额"},
			{name:"payAmount", checkType : "money", checkRule:"",  errorMsg:"输入合格付款金额"},
			{name:"invoAddress", checkType : "string", checkRule:"1,25",  errorMsg:"地址应为1-25个字符"},
		];
	export default {
		components: {
			wPicker,
			flowbefore,
		},
		data() {
			return {
				mode:'',//底部弹窗分类
				isSearchShow:false,
				disabled:false,//启用
				status:'',//流程状态
				beflows:{
					operstate:"",
					processId:''
				},
				conBill: {
					billNo: '',
					flowUrgent: '',
					flowTitle: jnpf.userInfo().realName+'的合同开票流程',
					drawer:'',
					billDate:'',
					companyName:'',
					conName:'',
					bank:'',
					amount:'',
					billAmount:'',
					taxId:'',
					invoiceId:'',
					invoAddress:'',
					detailed:'',
					payAmount:'',
					fileJson:''
				},
				fileList:[],
				
				fileJson:[],
				isBtnClicks:false,
				pickerList:[],
				urgentList:[{
					label: "普通",
					value:"1"
				},
				{
					label: "重要",
					value:"2"
				},
				{
					label: "紧急",
					value:"3"
				}],
				statusType:""
			}
		},
		onLoad(e) {
			/* 工作区进入状态 */
			let statusType = uni.getStorageSync("type")
			this.statusType = statusType
			/* 工作区进入状态 End*/
			let flowInfo= JSON.parse(e.flowInfo);
			this.beflows.operstate=flowInfo.operstate;
			if(flowInfo.operstate =='add'){
				this.conBill.flowId=flowInfo.id;
				this.status = 0;
				this.conBill.flowUrgent=helper.getSpinnerNew(this.urgentList,'1');
				helper.ajax({
					url: '/api/System/BillRule/BillNumber/WF_ConBillingNo',
					success: res => {
						this.conBill.billNo=res.data.data;
					}
				});
			}
			if(flowInfo.operstate=='info'){
				this.disabled=flowInfo.disabled;
				this.beflows.processId=flowInfo.id;
				this.status=flowInfo.status;
				helper.ajax({
					url: '/api/WorkFlowForm/ConBilling/' + flowInfo.id,
					success: res => {
						let _conBill=res.data.data;
						this.conBill = _conBill;
						this.conBill.flowUrgent=helper.getSpinnerNew(this.urgentList,_conBill.flowUrgent);
						this.conBill.billDate=helper.toDate(_conBill.billDate, 'yyyy-MM-dd HH:mm');
						if (_conBill.fileJson!="") {
							this.fileList=JSON.parse(_conBill.fileJson);
						}
					}
				});
			}
		},
		methods: {
			flowUrgent(e) {
				if(!this.disabled){
					this.pickerList = this.urgentList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.conBill.flowUrgent = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			billDate(e){
				if(!this.disabled){
					this.mode="dateTime";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.conBill.billDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			submit(e){
				this.conBill.fileJson = JSON.stringify(this.fileList)
				let formData=this.conBill;
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return
				}
				this.isBtnClicks=true;
				formData.flowUrgent=helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.status = 0;
				let method = "POST";
				let url = "/api/WorkFlowForm/ConBilling";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.billDate = Date.parse(formData.billDate);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
			save(e){
				this.conBill.fileJson = JSON.stringify(this.fileList)
				let formData=this.conBill;
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return
				}
				this.isBtnClicks=true;
				formData.flowUrgent=helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.status = 1;
				let method = "POST";
				let url = "/api/WorkFlowForm/ConBilling";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.billDate = Date.parse(formData.billDate);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {				
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
		},
	}
</script>

<style>
	.required{
		color: red;
	}
</style>
